/*
 * Created by IntelliJ IDEA.
 * User: rousseau
 * Date: May 13, 2002
 * Time: 12:00:52 AM
 * To change template for new class use 
 * Code Style | Class Templates options (Tools | IDE Options).
 */
package org.fractals;

import org.jfract.abstractBeans.AbstractFractalBean;
import org.jfract.util.Location;

import android.graphics.Color;

public class Markus
    extends AbstractFractalBean
{


  private static final long serialVersionUID = -1954923493449082281L;

  public Markus()
  {
    // super();
    setExpression("r*x*(1-x)");
    color = new int[256];
    for (int i = 0; i < 255; i++)
    {
      color[i] = Color.rgb(i, 0, 0);
    }
    color[255] = Color.rgb(0, 0, 255);
  }

  static
  {

  }

  private int max = 1000;

  public int getInit()
  {
    return init;
  }

  public void setRacine(String racine)
  {
    this.racine = racine;
  }

  public void setInit(int init)
  {
    this.init = init;
  }

  public String getRacine()
  {
    return racine;
  }

  public void setExpression(String expression)
      throws IllegalArgumentException
  {
    // parser = new org.nfunk.jep.JEP();
    // parser.addStandardFunctions();
    // parser.addStandardConstants();
    // parser.addVariable("x", 0);
    // parser.addVariable("r", 0);
    // parser.parseExpression(expression);
    // if( parser.hasError())
    // {

    // if (JFractGui.isInstanced()) {
    // JFractGui.showMessage("Error", parser.getErrorInfo());
    // }
    // else
    // throw new IllegalArgumentException(parser.getErrorInfo());
    // }
    // else
    // {
    this.expression = expression;
    // }
  }

  private String racine = "01";

  private int init = 20;

  private double x0 = .54;

  public int computeFractal(float x, float y)
  {
    // initialisation
    int racineLength = racine.length();
    double xt = x0;
    int i = 0;
    double exposant = 0;
    for (i = 0; i < init; i++)
    {
      double r;
      if (racine.charAt(i % racineLength) == '1')
        r = x;
      else
        r = y;

      xt = r * xt * (1 - xt);
      // xt = r*xt*(1-xt)* (Math.sin(1/(xt + .001)) + 1) / 2;
      // System.out.println(x);
    }
    // calcul de l'exposant
    double total = 0;
    for (i = init; i < iter + init; i++)
    {
      double r;
      if (racine.charAt(i % racineLength) == '1')
        r = x;
      else
        r = y;
      xt = r * xt * (1 - xt);
      // xt = r*xt*(1-xt)* (Math.sin(1/(xt + .001)) + 1) / 2;
      // xt=r*xt*(1-xt);
      total += java.lang.Math.log(java.lang.Math.abs(r - 2 * r * xt)) / java.lang.Math.log(2);
    }
    exposant = total / (iter - init);

    if (exposant > 0)
      return 0;
    else if (new Double(exposant).toString().equals("-Infinity"))
      return 255;

    // try
    // {
    // Thread.sleep(0,1);
    // }
    // catch (InterruptedException e)
    // {
    // }
    return new Double(java.lang.Math.exp(exposant) * (double) getColor().length).intValue();

  }

  public Location getDefaultLocation()
  {
    return new Location((float) 2, (float) 2, (float) .25);
  }

  @Override
  public void compute()
  {
    
  }

  @Override
  public void setValues(float[] values)
  {
  

  }
}
